package com.prpat;

import java.io.IOException;
import java.io.InputStream;

public class TaskCounter 
{
	private static int count = 0;
	
	public static int count(){
		synchronized (TaskCounter.class) {
			System.err.println("get = " +count);
			return count;
		}
	}
	
	public static void increment(){
		synchronized (TaskCounter.class) {
			count = count + 1 ;
			System.err.println("inc = " +count);
		}
	}
	
	public static void decrement(){
		synchronized (TaskCounter.class) {
			count = count - 1 ;
			System.err.println("dec = " +count);
		}
	}
	

	public static int getCount(){
		String st[]= {"ls", "files"};
		
		return terminal(st);
	}
	
	private static synchronized int terminal(String[] args)
	{
		StringBuilder output = new  StringBuilder();
		int ans = 0;
		try 
		{
			Process child = Runtime.getRuntime().exec(args);

			// Get the input stream and read from it the command output
			InputStream in = child.getInputStream();
			int c;

			while ((c = in.read()) != -1) 
				output.append((char)c);
			in.close();
			
			String[] lines = output.toString().split("\n");
			ans = lines.length;

		}
		catch (IOException e) 
		{
			System.err.println("TaskCounter, terminal, " + e.getMessage());
		}
		return ans;
	}

//	public static synchronized void increment(){
//		count++;
//	}

//	public static synchronized void decrement(){
//		count--;
//	}
}
